package cxf.client.rest;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.cxf.jaxrs.ext.form.Form;

import cxf.bean.SimpleBean;

public class RESTClient {
	public static void main(String[] args) {
		new RESTClient().sendRequest();
	}
	
	private void sendRequest() {
		HttpURLConnection conn = null;
		DataOutputStream out = null;
		
		try {
//			conn = getConnection("http://127.0.0.1:8080/cxfdemo/services/customerservice/addCustomer", "POST");
//			out = new DataOutputStream(conn.getOutputStream());
//			out.writeBytes("just test post input");
//			out.flush();
//			System.out.print("print post add response:");
//			getResponseFromInputStream(conn.getInputStream());
			
			// Post as JSON string
			/*
			WebClient client = WebClient.create("http://localhost:8989/CXFTest/cxfws/rest/restservice/json");
			client.type(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON);
			ArrayBean bean = new ArrayBean();
			Response r = client.post(bean);
			InputStream is = (InputStream)r.getEntity();
			getResponseFromInputStream(is);
			*/
			
			WebClient client = WebClient.create("http://localhost:8989/CXFTest/cxfws/rest/restservice/form");
			client.type(MediaType.APPLICATION_FORM_URLENCODED).accept(MediaType.TEXT_PLAIN);
			SimpleBean bean = new SimpleBean();
			bean.setName("blursss");
			bean.setAge(22);
			Form form = new Form();
			form.set("name", "formName");
			form.set("age", 122);
//			form.set("simpleBean", bean);
			Response r = client.form(form);
//			Response r = client.get();
			InputStream is = (InputStream)r.getEntity();
			getResponseFromInputStream(is);
			
//			conn = getConnection("http://localhost:8080/CXFTest/cxfws/rest/restservice/blurm/29", "GET");
//			conn.connect();
//			getResponseFromInputStream(conn.getInputStream());
			
//			conn = getConnection("http://localhost:8080/CXFTest/cxfws/rest/restservice/json", "GET");
//			conn.connect();
//			getResponseFromInputStream(conn.getInputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private HttpURLConnection getConnection(String urlString,String verb) {
		HttpURLConnection conn = null;
		try {
			URL url = new URL(urlString);
			conn = (HttpURLConnection)url.openConnection();
			conn.setRequestMethod(verb);
			conn.setDoInput(true);
			conn.setDoOutput(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}
	
	private void getResponseFromInputStream(InputStream is) {
		String resp = "";
		try {
			BufferedReader reader = new BufferedReader(new InputStreamReader(is));
			String next = null;
			while ((next = reader.readLine()) != null) {
				resp += next;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("the response is:" + resp);
	}
}
